﻿//*****************************************************************************
//
//  Silver Games - Checkers
//  Copyright © 2009 Randi Relander
//      <rjrelander@users.sourceforge.net>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation,either version 3 of the License,or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not,see <http://www.gnu.org/licenses/>.
//
//*****************************************************************************

using System;
using System.Windows;
using System.Windows.Controls;

namespace Checkers.Client
{
    public partial class Checker : UserControl
    {
        public Checker(Board boardView, Checkers.Common.Checker checkerModel)
        {
            InitializeComponent();

            mBoardView = boardView;

            mCheckerModel = checkerModel;

            IsHitTestVisible = false;

            mRedBase.Visibility = Visibility.Collapsed;
            mRedCrown.Visibility = Visibility.Collapsed;
            mBlackBase.Visibility = Visibility.Collapsed;
            mBlackCrown.Visibility = Visibility.Collapsed;

            if (mCheckerModel.Color == Checkers.Common.CheckerColor.Red)
            {
                mBase = mRedBase;
                mCrown = mRedCrown;
            }
            else
            {
                mBase = mBlackBase;
                mCrown = mBlackCrown;
            }

            mBase.Visibility = Visibility.Visible;

            mCheckerModel.MoveEvent += new Checkers.Common.Checker.MoveEventHandler(OnMove);
            mCheckerModel.AddCrownEvent += new EventHandler(OnAddCrownEvent);
            mCheckerModel.RemoveCrownEvent += new EventHandler(OnRemoveCrownEvent);
        }

        void OnMove(object sender, Checkers.Common.Checker.MoveEventArgs e)
        {
            Tile tile1 = mBoardView.TileModelToView(e.Tile1);
            Tile tile2 = mBoardView.TileModelToView(e.Tile2);

            if (tile2 != null)
            {
                Visibility = Visibility.Visible;

                SetPosition(tile2.Center);

                if (tile1 != null)
                {
                    double dx = tile1.Center.X - tile2.Center.X;
                    double dy = tile1.Center.Y - tile2.Center.Y;

                    double dist = Math.Sqrt(dx * dx + dy * dy * 4);

                    double velocity = 480.0;

                    double milliseconds = dist * 1000.0 / velocity;

                    TimeSpan dt = new TimeSpan(0, 0, 0, 0, (int)milliseconds);

                    mMoveX.From = dx;
                    mMoveY.From = dy;

                    mMoveX.Duration = new Duration(dt);
                    mMoveY.Duration = new Duration(dt);

                    mMoveAnimation.Completed += new EventHandler(OnMoveComplete);

                    mMoveAnimation.Begin();
                }
            }
            else
            {
                Visibility = Visibility.Collapsed;
            }
        }

        void OnMoveComplete(object sender, EventArgs e)
        {
            mCheckerModel.OnMoveComplete();
        }

        void OnAddCrownEvent(object sender, EventArgs e)
        {
            mCrown.Visibility = Visibility.Visible;
        }

        void OnRemoveCrownEvent(object sender, EventArgs e)
        {
            mCrown.Visibility = Visibility.Collapsed;
        }

        Board mBoardView;

        Checkers.Common.Checker mCheckerModel;

        public Checkers.Common.Checker Model { get { return mCheckerModel; } }

        UserControl mBase;
        UserControl mCrown;

        public void SetPosition(Point position)
        {
            SetValue(Canvas.LeftProperty, position.X);
            SetValue(Canvas.TopProperty, position.Y);
        }
    }
}
